package com.hongzhimei.yukicomic.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.hongzhimei.yukicomic.R;
import com.hongzhimei.yukicomic.adapter.HomeAdapter;
import com.hongzhimei.yukicomic.utils.GlideImageLoader;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：Nick CY on 2016/12/20 15:14
 * 邮箱：fsi000044@163.com
 */

public class HomeFrament extends BaseFragment {

    private ArrayList<String> listData;
    //第一页数据
    private int refreshTime = 0;


    @Bind(R.id.rlayout_search)
    RelativeLayout rlayoutSearch;
    @Bind(R.id.rl_blue)
    LinearLayout rlBlue;

    @Bind(R.id.xrecycle)
    XRecyclerView xrCycview;
    private HomeAdapter homeAdapter;
    private RelativeLayout head_banner;
    private RelativeLayout head_hot;
    private Banner fragmenthomeBanner;

    //当 fragment不可见时执行改方法
    @Override
    public void onInvisible() {

    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_home;
    }

    @Override
    public void initView(View view) {

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

        /**
         * 初始化轮播图数据
         */
        initBannerData();

        /**
         * 初始化热门周边 数据
         */
        initHotData();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        //首页轮播图
        head_banner = (RelativeLayout) View.inflate(getContext(), R.layout.home_head_banner, null);
        //首页限时抢购
        head_hot = (RelativeLayout) View.inflate(getContext(), R.layout.home_head_hot, null);
        xrCycview.addHeaderView(head_banner);
        xrCycview.addHeaderView(head_hot);
        return rootView;
    }

    /**
     * 初始化热门周边 数据
     */
    private void initHotData() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xrCycview.setLayoutManager(layoutManager);
        xrCycview.setNestedScrollingEnabled(false);
        xrCycview.setRefreshProgressStyle(ProgressStyle.BallTrianglePath);
        xrCycview.setLoadingMoreProgressStyle(ProgressStyle.Pacman);

//        mRecyclerView.setRefreshProgressStyle(ProgressStyle.TriangleSkewSpin);
//        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.SemiCircleSpin);
        xrCycview.setArrowImageView(R.mipmap.iconfont_downgrey);

        xrCycview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                refreshTime++;
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        listData.clear();
                        for (int i = 0; i < 15; i++) {
                            listData.add("和平一米五" + i + refreshTime);
                        }
                        homeAdapter.notifyDataSetChanged();
                        xrCycview.refreshComplete();
                    }

                }, 1000);            //refresh data here
            }


            @Override
            public void onLoadMore() {
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        for (int i = 0; i < 15; i++) {
                            listData.add("和平一米五" + i + refreshTime);
                        }
                        homeAdapter.notifyDataSetChanged();
                        xrCycview.loadMoreComplete();
                    }
                }, 1000);

            }
        });

        listData = new ArrayList<String>();
        homeAdapter = new HomeAdapter(listData);

        for (int i = 0; i < 15; i++) {
            listData.add("和平一米五" + i);
        }
        xrCycview.setAdapter(homeAdapter);

    }

    private void initBannerData() {
        fragmenthomeBanner = (Banner) head_banner.findViewById(R.id.fragment_home_banner);
        //初始化banner
        fragmenthomeBanner.setImageLoader(new GlideImageLoader());
        fragmenthomeBanner.setBannerStyle(BannerConfig.NUM_INDICATOR_TITLE);
        //假数据
        List<String> images = new ArrayList<>();
        List<String> titles = new ArrayList<>();
        images.add("http://ww1.sinaimg.cn/large/006eg253jw1fay8b6bbf4j30hs0hsabh.jpg");
        images.add("http://ww2.sinaimg.cn/large/0066cVpfjw1fay6kw3kbij30hs0hsn1r.jpg");
        images.add("http://ww3.sinaimg.cn/large/0066d6AWjw1faw40fd1wsj30hs0hsgn3.jpg");
        images.add("http://ww2.sinaimg.cn/large/0066cYcyjw1fay6a523a7j30hy0ciwja.jpg");
        titles.add("1月新番那么多，分分钟就告诉你有哪些好看！（上）");
        titles.add("天凉了，下雪了，送自己一辆兰博基尼吧");
        titles.add("上车吧！蛋黄带你萌穿越回去看上个世纪七十年代的妹子是啥样的");
        titles.add("被女票问到底有多长");
        fragmenthomeBanner.setImages(images).setBannerTitles(titles).setBannerAnimation(Transformer.CubeOut).setDelayTime(3000).start();

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


}
